package cn.acmsmu.shiro.test.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.acmsmu.mgsky1.shiro.service.LoginService;
import cn.acmsmu.mgsky1.shiro.util.TokenUtils;
import cn.acmsmu.shiro.test.param.UserLoginParam;


/**
 * @Desc:
 * @Author: huangzhiyuan
 * @CreateDate: 2023/5/20 20:37
 * @Modify:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private LoginService loginService;

    @GetMapping("/doLoginUI")
    public String doLoginUI() {
        return "login";
    }

    @PostMapping("/login")
    public Map doLogin(@RequestBody UserLoginParam param) {
        Map<String, String> resMap = new HashMap<>();
        Map<String, String> payLoad = new HashMap<>();
        payLoad.put("test", "test");
        String token = loginService.getToken(param.getUserName(), param.getPassword(), payLoad);
        resMap.put("msg", "login ok");
        resMap.put("token", token);
        System.out.println(TokenUtils.getTokenPayLoad(token));
        return resMap;
    }

    @PostMapping("/query")
    @RequiresPermissions(value = {"sys:user:query", "sys:admin"}, logical = Logical.OR)
    public String query() {
        System.out.println(TokenUtils.getCurrentLoginUserId());
        System.out.println(TokenUtils.getTokenFromHeader());
        return "query";
    }

}
